summaryrefslogtreecommitdiff
path: root/src/unitparser.cpp
blob: 43dacfc3eacba3558b8b91501c5609f9e33fabb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "unitparser.h"
#include "parser.h"
#include "lexer.h"

#include <bu/sio.h>
#include <bu/streamstack.h>
#include <bu/membuf.h>

UnitParser::UnitParser()
{
    setName("Parser");
    add( static_cast<Bu::UnitSuite::Test>(&UnitParser::order1),
            "order1", Bu::UnitSuite::expectPass );
}

UnitParser::~UnitParser()
{
}

Bu::String parse( const Bu::String sEq, int iScale=0, int iRadix=10 )
{
    Bu::MemBuf mbIn( sEq );
    Bu::MemBuf mbOut;
    Lexer lex( mbIn );
    lex.setScale( 5 );
    Parser parser( lex, mbOut );
    parser.parse();
    return mbOut.getString().trimWhitespace();
}

void UnitParser::order1()
{
    unitTest(parse("2+3*5") == "17");
    unitTest(parse("2+3-5") == "0");
    unitTest(parse("(2+3)*5") == "25");
    unitTest(parse("1.59*40/24*21", 5) == "55.65");
    unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619");
}